//
//  XBBarButtonItem.m
//  XianBai
//
//  Created by 相约在冬季 on 16/10/10.
//  Copyright © 2016年 aiyishu. All rights reserved.
//

#import "JTBarButtonItem.h"

#define IsStringEmpty(_ref)    (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref)isEqualToString:@""]) || [(_ref)isEqualToString:@"<null>"] || [(_ref)isEqualToString:@"(null)"]|| [(_ref)isEqualToString:@"null"]||(_ref.length == 0))

@implementation JTBarButtonItem

+ (instancetype)itemWithTitle:(NSString *)title
                        image:(NSString *)imageName
                        Style:(SNNavItemStyle)style
                       target:(id)target
                       action:(SEL)action
{
    if (style == SNNavItemStyleLeft) {
        //左侧按钮
        UIButton *btnLeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        btnLeftButton.backgroundColor = [UIColor clearColor];
        if(!IsStringEmpty(imageName)) {
            [btnLeftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [btnLeftButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
        }else if(IsStringEmpty(title)) {
            [btnLeftButton setImage:[UIImage imageNamed:@"gl_back"] forState:UIControlStateNormal];
            [btnLeftButton setImage:[UIImage imageNamed:@"gl_back"] forState:UIControlStateHighlighted];
        }else{
            [btnLeftButton setTitle:title forState:UIControlStateNormal];
            [btnLeftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        btnLeftButton.frame = CGRectMake(0, 0, 30, 30);
        btnLeftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 10);
        [btnLeftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        JTBarButtonItem *item = [[JTBarButtonItem alloc] initWithCustomView:btnLeftButton];
        return item;
    }else if(style == SNNavItemStyleRight) {
        //右侧按钮
        UIButton *btnRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        btnRightButton.backgroundColor = [UIColor clearColor];
        btnRightButton.frame = CGRectMake(0, 0, 60, 30);
        btnRightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
        [btnRightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        if(!IsStringEmpty(imageName)) {
            [btnRightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [btnRightButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateHighlighted];
        }else if(IsStringEmpty(title)) {
            [btnRightButton setImage:[UIImage imageNamed:@"right_share"] forState:UIControlStateNormal];
            [btnRightButton setImage:[UIImage imageNamed:@"right_share"] forState:UIControlStateHighlighted];
        }else{
            [btnRightButton setTitle:title forState:UIControlStateNormal];
            [btnRightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btnRightButton.frame = CGRectMake(0, 0, 40, 30);
        }
        
        [btnRightButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        JTBarButtonItem *item = [[JTBarButtonItem alloc] initWithCustomView:btnRightButton];
        return item;
    }else {
        return nil;
    }
}

- (void)setEnabled:(BOOL)enabled
{
    [super setEnabled:enabled];
    
    if ([self.customView isKindOfClass:[UIControl class]])
    {
        UIControl *ctrl = (UIControl *)self.customView;
        ctrl.enabled = enabled;
    }
}

- (void)setTintColor:(UIColor *)tintColor{
    [super setTintColor:tintColor];
    if ([self.customView isKindOfClass:[UIControl class]])
    {
        UIButton *ctrl = (UIButton *)self.customView;
        [ctrl setTitleColor:tintColor forState:UIControlStateNormal];
    }
}


@end
